package com.local.consumer.rabbitmq;

import com.local.consumer.service.OrderService;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class OrderConsumer {

    @Autowired
    private OrderService orderService;

    /**
     * 监听dead letter 死信消息队列，获取orderId，并将其状态
     * @param orderId
     */
    @RabbitListener(queuesToDeclare = @Queue("rabbitmq-demo.order-dead-letter-queue")) //当队列不存在时，自动创建队列
    public void orderListener(Integer orderId) {
        System.out.println("接收到消息OrderId:" + orderId);
        Integer updateResult = orderService.updateById(orderId);
        if (updateResult > 0) {
            System.out.println("消息OrderId:" + orderId + " 处理完成");
        } else {
            System.out.println("消息OrderId:" + orderId + " 处理失败");
        }
    }
}
